#coding: utf-8
#this one needs total rewrite
require 'boardparse'
require 'google'
require 'opts.rb'
WRONG_ARGUMENT = "Wrong argument, sorry"
class Execute
  def initialize
    @commands = {
      # values include aliases for command names
      :lastpost => {:aliases => %w[lastpost lp]},
      :randpost => {:aliases => %w[randpost rp]},
      :list => {:aliases => %w[list ls]},
      :googlesearch => {:aliases => %w[googlesearch gs]},
      :version => {:aliases => %w[version],:on_join => false},
      :wishmaster => {:aliases => %w[wishmaster admincp]},
      :nagisahelp => {:aliases => %w[nagisahelp]}
    }
    @default_chan = "0chan.ru"
  end 
  attr_reader :commands
  #run_command should always return String 
  #confid and cl are used to get version stanza
  def run_command text,confid,cl
    match = (text.match(/^\w+/))
    return "" if match.nil?
    command = match[0] 
    @found_command = nil
    @commands.each { |key,val| @found_command = key.to_s if val[:aliases].include? command}
    return "" until @found_command
    args_string = text.gsub(/^\w+/,'')  
    puts "args str = #{args_string}"
    case @found_command
    when "nagisahelp"
      msg = "nagisabot is under heavy development. Sources availabe at http://code.google.com/p/nagisabot/\n"
      msg += "Available commands: "
      @commands.each_key {|x| msg+= "#{x.to_s} "}
      msg +=". Type <command> --help for more information"
    when "version"
      nick = args_string[1..-1]
      puts "!!!!!!!!!!!!#{nick}     #{confid}"
      do_version nick,confid,cl
    else
      checkopts = Commopts.new @found_command, @default_chan 
      args_string.gsub!(/[[:blank:]]+/,' ')
      args_array = args_string.split ' '
      options = checkopts.parse args_array
      #if options.class is String, then some error occurred
      return options if options.class == String
      meth = self.method "do_#{@found_command}"
      meth.call(options,args_array)
    end
  end
  def do_version nick,confid,cl
    sleep 1
    iq = Jabber::Iq.new(:get, "#{confid}/#{nick}")
    # and ask for the version
    iq.query = Jabber::Version::IqQueryVersion.new
    cl.send(iq)
    return ""
  end
  def do_lastpost options,args,randpost=false
    return "Wrong chan argument" if options.chan.nil?
    args = args.join(' ')
    if args =~ /^\/?(_?[[:alpha:]]+)\/?$/
      section = $1
      chan = options.chan
      ch = ImageBoard.new chan
      randpost ? post = ch.randpost(section) : post = ch.lastpost(section)
      return post if post.class == String
      return "#{post[:link]}#{post[:text]}"
    else
      return WRONG_ARGUMENT
    end
  end

  def do_randpost options,args
    do_lastpost options,args,true
  end

  def do_list options,args
    return "Wrong chan argument" if options.chan.nil?
    return "Wrong count argument" if options.count.nil?
    args = args.join(' ')
    if args =~ /^\/?(_?[[:alpha:]]+)\/?$/
      section = $1
      n = options.count
      chan = options.chan
      ch = ImageBoard.new chan
      result = ch.list section,n
    else
      return WRONG_ARGUMENT
    end
  end
  def do_wishmaster options, args
   return "FISHMASTER!!!" if args.length > 0
   
   (@default_chan = options.defaultchan; return "Default chan successfully changed to #{@default_chan}") if options.defaultchan
   ""
  end
  def do_googlesearch options,args
    args = args.join(' ')
    answer = wtf(args)
    "#{answer[:url]} : #{answer[:text]}"
  end
end
